/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Jan 28, 2011
 * @Createtime 9:57:38 PM
 */

package com.tmh.Away3dLite
{
	import away3dlite.cameras.HoverCamera3D;
	import away3dlite.containers.Scene3D;
	import away3dlite.containers.View3D;
	import away3dlite.core.base.MeshSortType;
	import away3dlite.core.render.BasicRenderer;
	import away3dlite.events.Loader3DEvent;
	import away3dlite.loaders.Loader3D;
	import away3dlite.loaders.Max3DS;
	import away3dlite.materials.BitmapFileMaterial;
	import away3dlite.materials.WireColorMaterial;
	import away3dlite.materials.WireframeMaterial;
	import away3dlite.primitives.Cube6;
	import away3dlite.primitives.Plane;
	import away3dlite.primitives.Sphere;
	import away3dlite.primitives.Torus;

	import flash.display.Sprite;
	import flash.events.Event;

	import net.hires.debug.Stats;

	public class Main extends Sprite
	{
		private var scene:Scene3D;

		private var camera:HoverCamera3D;

		private var renderer:BasicRenderer;

		private var view:View3D;

		private var plane:Plane;

		private var sphere:Sphere;

		private var cube:Cube6;

		private var torus:Torus;

		public function Main()
		{
			init();
		}

		private function init():void
		{
			initEngine();
			initObjects();

			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}

		private function addedToStageHandler(event:Event):void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function initEngine():void
		{
			scene = new Scene3D();

			camera = new HoverCamera3D();
			camera.distance = 1500;
			camera.panAngle = 180;
			camera.tiltAngle = 15;

			renderer = new BasicRenderer();

			view = new View3D();
			view.x = 400;
			view.y = 250;
			view.scene = scene;
			view.camera = camera;
			view.renderer = renderer;

			addChild(view);

			addChild(new Stats());
		}

		private function initObjects():void
		{
			plane = new Plane();
			plane.y = -20;
			plane.width = 1000;
			plane.height = 1000;
			plane.sortType = MeshSortType.BACK;
			plane.segmentsW = 20;
			plane.segmentsH = 20;

			//scene.addChild(plane);

			sphere = new Sphere();
			sphere.x = 300;
			sphere.y = -160;
			sphere.z = 300;
			sphere.radius = 150;
			sphere.segmentsW = 12;
			sphere.segmentsH = 10;

			//scene.addChild(sphere);

			cube = new Cube6();
			cube.x = 300;
			cube.y = -160;
			cube.z = -80;
			cube.width = 200;
			cube.height = 200;
			cube.depth = 200;

			//scene.addChild(cube);

			torus = new Torus();
			torus.x = -250;
			torus.y = -160;
			torus.z = -250;
			torus.radius = 150;
			torus.tube = 60;
			torus.segmentsR = 12;
			torus.segmentsT = 10;

			//scene.addChild(torus);

			var loader:Loader3D = new Loader3D();
			var max3DS:Max3DS = new Max3DS();
			max3DS.material = new BitmapFileMaterial("assets/Suzuki Katana Bike.jpg");
			loader.loadGeometry("assets/Suzuki Katana Bike.3ds", max3DS);
			loader.addEventListener(Loader3DEvent.LOAD_ERROR, loadError);
			loader.scaleX = 3;
			loader.scaleY = 3;
			loader.scaleZ = 3;
			loader.rotationX = -90;
			loader.rotationY = 135;
			scene.addChild(loader);
		}

		private function loadError(event:Loader3DEvent):void
		{

		}

		/**
		 * Navigation and render loop
		 */
		private function onEnterFrame(event:Event):void
		{
			camera.hover();
			view.render();
		}
	}
}